#!/usr/bin/env python2.7
"""
Example solution to the introductory exercise from Data Structures I.
How to use it: use "echo xxx >> data.txt" to append data to a file and then
* under Windows: type data.txt | insertion.py [-n]
* under UNIX-like: cat data.txt | insertion.py [-n]
Use the -n flag to handle data as numbers (not just strings).
"""
import sys

def insertionSort(arr):
	"""
	Basic insertion sort implementation.
	Returns the number of moves in the inner loop.
	"""
	ct = 0
	for i in range(1, len(arr)):
		cur = arr[i]
		j = i-1
		while j>=0 and arr[j]>cur:
			arr[j+1] = arr[j]
			ct += 1
			j -= 1
		arr[j+1] = cur
	return ct

if len(sys.argv)==2 and (sys.argv[1]=="--help" or sys.argv[1]=="-h"):
	print __doc__.strip()
else:
	arr = []
	for x in sys.stdin.readlines():
		x = x.strip()
		if "-n" in sys.argv:
			x = int(x)
		arr.append(x)
	print insertionSort(arr)
	
